package com.weathereport.activity.adapter;

import java.util.List;

import com.weathereport.R;
import com.weathereport.entity.Local;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class WeatheReportMainAdapter extends BaseAdapter{

	private Context context;
	private List<Local> locals;

	public WeatheReportMainAdapter(Context context, List<Local> locals) {
		this.context = context;
		this.locals = locals;
	}
	
	public int getCount() {
		if (localsIsNotNull()) {
			return locals.size();
		}
		return 0;
	}

	public Object getItem(int position) {
		if (localsIsNotNull()) {
			return locals.get(position);
		}
		return null;
	}

	private boolean localsIsNotNull() {
		if (locals != null && !locals.isEmpty()) {
			return true;
		}
		return false;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View arg1, ViewGroup arg2) {
		Local local = locals.get(position);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v 					= inflater.inflate(R.layout.list_weathereportmain, null);		
		TextView name 			= (TextView) v.findViewById(R.id.name);
		TextView weather 		= (TextView) v.findViewById(R.id.weather);
		TextView tempMin 		= (TextView) v.findViewById(R.id.tempMin);
		TextView tempMax 		= (TextView) v.findViewById(R.id.tempMax);
				
		name.setText(local.getName().toString().trim());
		
		if (local.getForecast().size() > 0){
			
			weather.setText(local.getForecast().get(0).getWeather().toString().trim());
			tempMin.setText(local.getForecast().get(0).getLowTemp().toString().toString().trim());
			tempMin.setTextColor(setColor(Integer.parseInt(tempMin.getText().toString())));
			tempMax.setText(local.getForecast().get(0).getHighTemp().toString().toString().trim());
			tempMax.setTextColor(setColor(Integer.parseInt(tempMax.getText().toString())));
		}else{
			
			weather.setText(R.string.noData);
			tempMin.setText("-");			
			tempMax.setText("-");			
		}
		return v;
	}
	
	public int setColor(int color){
		
		int result = 0;
		
		if (color <= 5){
			
			result = Color.BLUE;
		}
		
		if (color >= 6 && color <=14){
			
			result = Color.rgb(135, 206, 250);
		}
		
		if (color >= 15 && color <= 20){
			
			result = Color.GREEN;
		}
		
		if (color >=21 && color <= 30){
			
			result = Color.rgb(255, 165, 0);
		}
		
		if (color >= 31){
			
			result = Color.RED;
		}
		
		return result;
	}
}
